// jjj project main.go
package main

import (
	"fmt"
	"gitee.com/jjj-server/posts"
	"gitee.com/jjj-server/session"
	"gitee.com/jjj-server/user"
	"github.com/labstack/echo"
	"log"
	"net/http"
	"strconv"
	"time"
)

func main() {
	fmt.Println("# JianJiJi Server Startd at: " + time.Now().Format("2006-01-02 15:04:05"))
	e := echo.New()
	// 添加用户
	e.POST("/user/add",func(c echo.Context)error{
		username := c.FormValue("username")
		password := c.FormValue("password")
		role,_ := strconv.Atoi(c.FormValue("role"))
		success := strconv.Itoa(user.AddUser(username,password,role))
		return c.String(http.StatusOK,success)	// 返回操作码
	})

	// 删除用户
	e.POST("/user/del",func(c echo.Context)error{
		success := strconv.Itoa(user.DelUser(c.FormValue("username")))
		return c.String(http.StatusOK,success)
	})

	// 用户登录
	e.POST("/user/login",func(c echo.Context)error{
		log.Println(c.Request().Body)
		log.Println("Now login: ")
		log.Println(c.FormValue("username")+"Password:"+c.FormValue("password"))
		success := session.Login(c.FormValue("username"),c.FormValue("password"))

		if !success{
			log.Print("Failed to login.")
			return c.String(http.StatusNotFound,"Failed to login.")
		}
		return c.String(http.StatusOK,"Login successfully.")
	})

	// 用户注销
	e.POST("/user/unlogin",func(c echo.Context)error{
		success := session.Unlogin(c.FormValue("session_key"))
		if !success{
			log.Print("Failed to unlogin.")
			return c.String(http.StatusNotFound,"Failed to unlogin.")
		}
		return c.String(http.StatusOK,"Login successfully.")
	})

	// 编辑个人信息
	// 0:Nickname,1:Realname,2:Sex,3:Age,4:Phone,5:QQ
	e.POST("/user/profile/edit/:type",func(c echo.Context)error{
		value := c.FormValue("value")
		var status  = -1
		switch c.Param("type") {
		case "0":	// 修改昵称
			status = user.UpdateUserNickName(value)
			break
		case "1":	// 修改真名
			status = user.UpdateUserRealName(value)
			break
		case "2":	// 修改性别
			age,_ := strconv.Atoi(value)
			status = user.UpdateUserSex(age)
			break
		case "3":	// 修改年龄
			sex,_ := strconv.Atoi(value)
			if sex >=0 || sex <= 1{
				status = user.UpdateUserSex(sex)
				break
			}

		case "4":	// 修改手机
			status = user.UpdateUserPhone(value)
			break
		case "5":	// 修改QQ
			status = user.UpdateUserQQ(value)
			break
		default:	// 修改整套（需要格式）
			profile := user.UserProfile{}
			if err := c.Bind(profile); err != nil{
				log.Println("Failed to bind profile: ",err)
				c.String(http.StatusInternalServerError,"false")
			}

			status = user.UpdateUserProfile(profile)

		}
		return c.String(http.StatusOK,strconv.Itoa(status))
	})


// 兼职
	// 添加兼职
	e.POST("/job/add", func(c echo.Context) error {
		type New_Job struct {
			Title string `json:"title"`
			Publisher string `json:"publisher"`
			Content string `json:"content"`
		}
		job := New_Job{}
		if err := c.Bind(job);err !=nil{
			log.Println("<< Failed to add job: ",err)
			return c.String(http.StatusInternalServerError,"")
		}
		success := posts.AddJob(job.Title, job.Publisher,job.Content)
		if success != true{
			log.Println("<< Failed to add job: ",success)
			return c.String(http.StatusInternalServerError,"")
		}
		log.Println("<< Added Job: ",success)
		return c.String(http.StatusOK,"true")
	})

	// 获取兼职信息:ID
	e.GET("/job/:id", func(c echo.Context) error {
		jid := c.Param("id")
		job := posts.GetJobById(jid)
		return c.JSON(http.StatusOK,job)
	})



	// 获取最近的兼职信息
	e.GET("/job/recent", func(c echo.Context) error {
		job := posts.GetJobRecentFive()
		type Jobs struct {
			Jobs []posts.Job `json:"jobs"`
		}

		return c.JSON(http.StatusOK,Jobs{job})
	})

	// 下拉次数获取兼职信息（需要传递你是第几次下拉）
	/*e.GET("/job/slide/:time", func(c echo.Context) error {
		//t,_ := strconv.Atoi(c.Param("time"))
		//job := posts.GetJobSildeFive(t)
		//return c.JSON(http.StatusOK,job)
	})*/
	// 删除指定ID兼职
	e.GET("/job/del/:id", func(c echo.Context) error {
		success := posts.DelJob(c.Param("id"))
		if success != true{
			log.Println("Failed to del job: ",success)
			return c.String(http.StatusInternalServerError,"")
		}
		return c.String(http.StatusOK,"true")
	})
	e.Logger.Fatal(e.Start(":8686"))
}


func CheckPrev(token string){

}